//
//  Utils.swift
//  cangchu
//
//  Created by kang on 2021/11/4.
//

import UIKit
import Foundation

func UIColorFromRGB(rgbValue: UInt) -> UIColor {
    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}
// 状态栏高度
let statusHeight = isIphoneX() ? 44 : 20;
// 判断是否设备是iphonex系列
func isIphoneX() -> (Bool) {
    // iPhoneX,XS
    if (UIScreen.main.bounds.size.width == 375 && UIScreen.main.bounds.size.height == 812) {
        return true;
    }
    // iPhoneXS Max,XR
    if (UIScreen.main.bounds.size.width == 414 && UIScreen.main.bounds.size.height == 896) {
        return true;
    }
    return false;
}

//屏幕高度
let screenHeight = UIScreen.main.bounds.height
//屏幕宽度
let screenWidth = UIScreen.main.bounds.width

func toJSonStr(element:Any) -> String {
    
    if JSONSerialization.isValidJSONObject(element){
        
        guard let data = try? JSONSerialization.data(withJSONObject: element, options: .prettyPrinted) else{return ""}
        
        let jsonStr = String.init(data: data, encoding: .utf8)
        return jsonStr ?? ""
    }
    return ""
}

/**
 实体转字符串
 */
func model2String<T>(_ model:T) ->String where T:Encodable{
    let encoder = JSONEncoder()
    guard let json = try? encoder.encode(model)else{return ""}
    let modeStr = String.init(data: json, encoding: .utf8)
    return modeStr!
}

/**
 字符串转实体
 */
func json2Model<T>(_ model:T.Type,json:String)->T where T:Decodable{
    let decoder = JSONDecoder()
    let jsonData = json.data(using: String.Encoding.utf8)
    let entity = try? decoder.decode(T.self, from: jsonData!)
    return entity!
}

func NetIsConnect()->Bool{
    let status = Reach().connectionStatus()
    
    switch status {
    case .offline:
        return false
    case .online(_):
        return true
    case .unknown:
        return false
    }
}

func isMobile(phoneNum:String)-> Bool {
    let phoneRegex = "^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(17[0,0-9]))\\d{8}$"
    let phoneTest = NSPredicate(format: "SELF MATCHES %@" , phoneRegex)
    return (phoneTest.evaluate(with: phoneNum));

}

func getString(key:String)->Any?{
    let diaryList = Bundle.main.path(forResource: "strings", ofType:"plist")!
    let data:NSMutableDictionary = NSMutableDictionary(contentsOfFile: diaryList)!
    return data[key]
}

func formatTime(time:String,defaultFormat:String,showFormat:String)->String{
    if time == "" {return ""}
    let df = DateFormatter()
    df.dateFormat = defaultFormat
    let dfDate = df.date(from: time)!
    
    df.dateFormat = showFormat
    return df.string(from: dfDate)
}

/**
 毫秒级时间戳
 */
func milliStamp()->String {
    let date = Date()
    let timeInterval: TimeInterval = date.timeIntervalSince1970
    let millisecond = CLongLong(round(timeInterval*1000))
    return "\(millisecond)"
}

/**
 秒级时间戳
 */
var timeStamp : String {
    let date = Date()
    let timeInterval: TimeInterval = date.timeIntervalSince1970
    let timeStamp = Int(timeInterval)
    return "\(timeStamp)"
}
